<?php

namespace App\Http\Controllers\Admin\Group;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\AdminController;
use App\Models\Project;

class ProjectController extends AdminController
{
    // 规定为空参数
	protected function rules($methodname)
    {
        switch ($methodname) {
            case 'projectList':
               	$params = ['token'=>'required'];
                break;
            case 'details':
                $params = ['project_id'=>'required|numeric',
                		   'token'=>'required'];
                break;
            case 'addProject':
            	$params=[
            		'token'=>'required',
            		'title'=>'required',
            		'content'=>'required',
            		'progress'=>'required',
            		'project_type'=>'required|numeric',
            		'donation'=>'required|numeric',
            		'money'=>'required',
            		'province'=>'required',
            		'city'=>'required',
            		'district'=>'required',
            		'location'=>'required',
            		'cover'=>'required',
            		'end_time'=>'required',
            		'liable'=>'required',
            		'mobile'=>'required',
            		'grade'=>'required|numeric',
            		// 'project_book'=>'required',
            		];
            	break;
        }
        return $params;
    }
    //项目列表页
	function projectList(Request $request){
		$userInfo = json_decode(crypt::decrypt($request->input('token')));
		$limit=10;
		$db=Project::select('*');
		$db->where('group_id',1);//$userInfo->group_id
		if(intval($request->input('limit'))){
			$limit=intval($request->input('limit'));
		}
		$page=intval($request->input('page'));
		if(empty($request->input('page'))){
			$page=0;
		}
		$offset=$page*$limit;
		$db->orderBy('created_at','desc');
		$db->offset($offset);
		$db->limit($limit);
		$projectList=$db->get();
		if(count($projectList)){
			return array('code' => config('constants.API_NOT_EXISTS_CODE'),'message' => $projectList);
		}else{
			return ['code' => config('constants.API_NULL_CODE'), 'message' => config('constants.API_NULL')];
		}
	}
	// 项目详情接口
	function details(Request $request){
		$userInfo = json_decode(crypt::decrypt($request->input('token')));
		$first=project::select('*')
		->where('group_id',1)//$userInfo->id
    	->where('id',$request->input('project_id'))
    	->first();
		if($first){
			return ['code' => config('constants.API_SUCCESS_CODE'), 'message' => $first];
		}else{
			return ['code' => config('constants.API_NULL_CODE'), 'message' => config('constants.API_NULL')];
		}
	}
	// 提交项目审核接口
	function addProject(Request $request){
		$userInfo = json_decode(crypt::decrypt($request->input('token')));
		$data=array(
            'title' => $request->input('title'), 
            'content' => $request->input('content'), 
            'progress' => $request->input('progress'), 
            'project_type' => $request->input('project_type'), 
            'donation' => $request->input('donation'), 
            'location' => $request->input('location'), 
            'money' => $request->input('money'), 
            'group_id' => $userInfo->id, 
            'province' => $request->input('province'), 
            'city' => $request->input('city'), 
            'district' => $request->input('district'), 
            'cover' => $request->input('cover'), 
            'end_time' => $request->input('end_time'), 
            'liable' => $request->input('liable'), 
            'mobile' => $request->input('mobile'), 
            'grade' => $request->input('grade'), 
            // 'project_book' => $request->input('project_book'), 
            'status' =>config('constants.ADMIN_PROJECT_STATUS'), 
            );
        $temp=Project::create($data);
        if($temp){
            return response()->json(['code'=>config('constants.API_SUCCESS_CODE'),'message'=>config('constants.API_SUCCESS')]);
        }
        return response()->json(['code'=>config('constants.API_PARAM_ERROR_CODE'),'message'=>$validator->errors()->first()]);
	}
}
